package com.yan.deezer.activity;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.yan.deezer.R;

public class ImageViewUpdater extends YansBetterAsyncTask<String, Void, Bitmap> {
	private static final String TAG = "IMAGE_VIEW_UPDATER";
	private int imgID;
	private ImageView mImageView;
	
	public ImageViewUpdater(ImageView view, int id){
		imgID = id;
		mImageView = view;
	}
	
	@Override
	protected Bitmap doInBackground(String... params) {
		Bitmap b = null;
		try 
		{
			URL url = new URL(params[0]);
			b = BitmapFactory.decodeStream(url.openStream());
		} 
		catch (MalformedURLException e) 
		{
			Debug.debugWarning(TAG, "MalformedURLException : " +params[0]);
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			Debug.debugWarning(TAG, "IOException : " +params[0]);
			e.printStackTrace();
		}
		DataManager.getInstance().addBitmap(imgID, b);
		return b;
	}
	 
	@Override
    protected void onPostExecute(Bitmap result) {
		if(mImageView == null)
			return;
		
		else if(mImageView != null&&result==null){
			
			mImageView.setImageResource(R.drawable.ic_launcher);

		}
		
		if (((Integer)mImageView.getTag()) == imgID){
			mImageView.setImageBitmap(result);
		}		
    }
	
	

}
